//app.js
var { host } =require("./config");
var that ; 
App({
  onLaunch: function () {
    that=this;
    this.login();
  },
  user:null,
  open_id:null,
  post_ajax:function(param){
    var data = param.data||{},
        success = param.success,
        url = param.url || "/xcx/test";
        url = host + `/xcx/${url}` ;
    if (that.open_id) {
      data.open_id = that.open_id
    }
    wx.request({
      url: url, // 仅为示例，并非真实的接口地址
      data: data,
      method:"POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {
        success(res.data)
      }
    })
  },
  login:function(){
    if (!this.open_id){
      wx.login({
        success(res) {
          if (res.code) {
             var data ={} ;
              data.url = "login";
            data.data = {code: res.code };
            data.success = function(res){
              that.open_id = res.data.openid
            }
            that.post_ajax(data)
          } else {
            console.log('调用失败' + res.errMsg)
          }
        }
      })
    }

  },

})
